import * as XLSX from "xlsx";
export function exportToExcel(data, fileName) {
  // 导出
  // 将数据转换为工作表
  const worksheet = XLSX.utils.json_to_sheet(data);
  // 创建工作簿并添加工作表
  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
  // 生成Excel文件
  const excelBuffer = XLSX.write(workbook, {
    bookType: "xlsx",
    type: "array",
  });
  // 创建Blob对象
  const dataBlob = new Blob([excelBuffer], {
    type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8",
  });
  // 创建链接并模拟点击下载
  const downloadLink = document.createElement("a");
  downloadLink.href = window.URL.createObjectURL(dataBlob);
  downloadLink.download = fileName + ".xlsx";
  document.body.appendChild(downloadLink);
  downloadLink.click();
  document.body.removeChild(downloadLink);
}